﻿namespace Collaboris.Saf.Agent.Tests
{
    using System;
    using System.IO;
    using System.Xml;

    using Engine;
    using Engine.Report;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class SafServiceTests
    {
        public const string FeaturesUrl = @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES\";

        private static Collaboris.Saf.Agent.Tests.Support.Entity.FileInfo fileInfo;

        private string macroXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
                                + "<objects xmlns=\"http://www.springframework.net\">"
                                + "<object id=\"MacroToProcess\" type=\"Macro\" >"
                                + "  <constructor-arg name=\"id\" value=\"{41C84942-B11C-4acf-B773-00FA3FF53750}\" />"
                                + "  <constructor-arg name=\"actions\">"
                                + "    <list element-type=\"IAction\">"
                                + "      <object name=\"CreateFile\" type=\"Collaboris.Saf.Agent.Tests.Support.Actions.CreateFile, Collaboris.Saf.Agent.Tests, Version=2.0.0.0, Culture=neutral, PublicKeyToken=182db3eac6a9e195\">"
                                + "        <property name=\"EntityList\" ref=\"EntListing\"/>"
                                + "      </object>"
                                + "    </list>"
                                + "  </constructor-arg>" + "</object>"
                                + "  <object id=\"EntListing\" type=\"Entities\">"
                                + "    <constructor-arg>"
                                + "      <list>"
                                + "        <object type=\"Collaboris.Saf.Agent.Tests.Support.Entity.FileInfo, Collaboris.Saf.Agent.Tests, Version=2.0.0.0, Culture=neutral, PublicKeyToken=182db3eac6a9e195\">"
                                + "          <property name=\"FilePath\" value=\"C:\\Temp\\Collaboris.Saf.Agent.Tests.Support.Entity.FileInfo.unitText\" />"
                                + "        </object>"
                                + "      </list>"
                                + "    </constructor-arg>"
                                + "  </object>"
                                + "</objects>";
        
        /// <summary>
        /// Runs Before all tests.
        /// </summary>
        /// <param name="testContext">The test context.</param>
        [ClassInitialize()]
        public static void BeforeAllTests(TestContext testContext)
        {
            fileInfo = new Collaboris.Saf.Agent.Tests.Support.Entity.FileInfo();
            if (!Directory.Exists(@"c:\Temp"))
            {
                Directory.CreateDirectory(@"c:\temp");
            }

            fileInfo.FilePath = @"C:\Temp\Collaboris.Saf.Agent.Tests.Support.Entity.FileInfo.unitText";
            if (File.Exists(fileInfo.FilePath))
            {
                File.Delete(fileInfo.FilePath);
            }
        }

        [TestMethod]
        public void TestCreatingAFileRemote()
        {
            Saf service = new Saf();

            MacroReport report = service.RunMacro("http://localhost:3322", ProcessMode.Do, macroXml);

            // does the file now exist ?
            Assert.IsTrue(File.Exists(fileInfo.FilePath));
        }

        [TestMethod]
        public void TestSimpleRun()
        {
            Saf service = new Saf();
            XmlDocument macroXd = new XmlDocument();
            macroXd.Load(FeaturesUrl + @"WSS.Web.CreateWeb\Macro.xml");

            MacroReport report = service.RunMacro("http://localhost:3322", ProcessMode.Do, macroXd.OuterXml);
        }

        [TestMethod]
        public void TestInvalidXml()
        {
            Saf service = new Saf();
            try
            {
                MacroReport report = service.RunMacro("http://localhost:3322", ProcessMode.Do, "<objects/>");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("No object named"));       
            }
        }
    }
}
